package com.example.hszpad;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.hszpad.service.ModbusSlaveService;

public class MainDemoActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainDemoActivity";
    private ModbusSlaveService modbusSlaveService;
    private ServiceConnection serviceConnection;
    private Button btn_01;
    private Button btn_02;
    private Button btn_03;
    private Button btn_04;
    private Button btn_05;
    private Button btn_06;
    private Button btn_07;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_demo);
        btn_01 = findViewById(R.id.btn_01);
        btn_02 = findViewById(R.id.btn_02);
        btn_03 = findViewById(R.id.btn_03);
        btn_04 = findViewById(R.id.btn_04);
        btn_05 = findViewById(R.id.btn_05);
        btn_06 = findViewById(R.id.btn_06);
        btn_07 = findViewById(R.id.btn_07);
        btn_01.setOnClickListener(this);
        btn_02.setOnClickListener(this);
        btn_03.setOnClickListener(this);
        btn_04.setOnClickListener(this);
        btn_05.setOnClickListener(this);
        btn_06.setOnClickListener(this);
        btn_07.setOnClickListener(this);

        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                ModbusSlaveService.LocalBinder binder = (ModbusSlaveService.LocalBinder) service;
                modbusSlaveService = binder.getServices();
                modbusSlaveService.myway();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                modbusSlaveService = null;
                Log.e(TAG, "onServiceDisconnected:");

            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_01:
                Intent start = new Intent(this, ModbusSlaveService.class);
                startService(start);
                break;
            case R.id.btn_02:
                Intent stop = new Intent(this, ModbusSlaveService.class);
                stopService(stop);
                break;
            case R.id.btn_03:
                //绑定服务
                Intent intent = new Intent(this, ModbusSlaveService.class);
                bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
                break;
            case R.id.btn_04:
                //解绑服务
                Intent intent1 = new Intent(this, ModbusSlaveService.class);
                unbindService(serviceConnection);
                break;
            case R.id.btn_05:
                //解绑服务
                if (modbusSlaveService != null) {
                    modbusSlaveService.start();
                }
                break;
            case R.id.btn_06:
                //解绑服务
                if (modbusSlaveService != null) {
                    modbusSlaveService.stop();
                }
                break;
            case R.id.btn_07:
                //解绑服务
                if (modbusSlaveService != null) {
//                    modbusSlaveService.generateVal();
                }
                break;
            default:
                break;
        }


    }
}
